#include <QApplication>
#include <QMessageBox>
#include <QFileInfo>
#include <QDir>
#include <QFileDialog>

//次应用测试自己写的DLL是否能加载成功

#include <QLibrary>

typedef void * (*GetDll)(void);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    //指定路径
    QDir *dir = new QDir("C:/QtTest");

    //文件类型过滤
    QStringList filter;
    filter<<"*.dll";
    //dir->setNameFilters(filter);

    //获取目录下的dll文件
    QList<QFileInfo> *fileInfo = new QList<QFileInfo>(dir->entryInfoList(filter));

    //没有dll文件
    if(0 == fileInfo->count()){
        QMessageBox::about(NULL, "QLibrary load DLL file", "Can not find dynamic link library file!");
        return -1;
    }

    //获取exe运行文件目录
//    QString strExePath = QApplication::applicationDirPath();
//    QLibrary rLib(strExePath + "/test.dll");

    QLibrary rLib(fileInfo->at(0).absoluteFilePath());

    qDebug("loading test.dll...");
    if(rLib.load()){
        GetDll pFun = (GetDll)rLib.resolve("getDll");
        qDebug("resolving getDll() function ...");
        if(pFun)
        {
            pFun();
            qDebug("test.dll load ok!");
            QMessageBox::about(NULL, "QLibrary load DLL file", fileInfo->at(0).fileName() + " load successed!!");
        }else{
            qDebug("test.dll load ok!");
            QMessageBox::about(NULL, "QLibrary load DLL file", fileInfo->at(0).fileName() + " load successed, but getDll() resolve failed!");
        }

    }
    else{
        qDebug(rLib.errorString().toLatin1().data());
        QMessageBox::about(NULL, "QLibrary load DLL file", fileInfo->at(0).fileName() + " load failed!!");
    }

    return 0;
}


